package com.mall.util;


import com.cloopen.rest.sdk.CCPRestSmsSDK;

import java.util.*;

/**
 * 短信发送工具包
 */
public class SmsUtil {

    private static final String serverIp = "app.cloopen.com";
    private static final String serverPort = "8883";
    private static final String accountId = "8a216da87ba59937017be70fdf6b0e76";
    private static final String accountToken = "1b428229c7e7499bbce0218139cecf57";
    private static final String appId = "8a216da87ba59937017be70fe07e0e7c";

    public static List< Map<String,Object>> sendSmsToMobile( List<String> mobiles,String templateId,String []datas){

        List< Map<String,Object>> list = new ArrayList<>();

        Map<String,Object> result = null;//用于接收短信发送的结果
        CCPRestSmsSDK api = new CCPRestSmsSDK();
        api.init(serverIp,serverPort);
        api.setAccount(accountId,accountToken);
        api.setAppId(appId);


        for (String to:mobiles) {
            Map<String,Object> map = new HashMap<>();

            //执行发送
            result = api.sendTemplateSMS(to, templateId, datas);
            String code = result.get("statusCode").toString();
            String msg = "";
            if ("000000".equals(code)){
                System.out.println("发送信息："+result.get("data"));
                msg = String.format("信息码:%s,信息:%s",code,result.get("statusMsg"));
                map.put("is",true);
            }else {
                msg = String.format("错误码:%s,错误信息:%s",code,result.get("statusMsg"));
                System.out.println(msg);
                map.put("is",false);

            }
            map.put("time",new Date().getTime());
            map.put(to,msg);
            list.add(map);
        }
        return list;
    }

    public static void main(String[] args) {
/*
        String moobiles[] = {"18775811937"};
        String datas[] = {"888888","5"};//发送内容,有效时间

        List<Map<String,Object>> list = sendSmsToMobile(moobiles,"1",datas);
        for (Map<String,Object> map:list){
            System.out.println(map.toString());
        }

*/


    }

}
